package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ChanceDelivery;
import com.sl.au.entity.ChanceManagerDelivery;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.entity.Chance;
import com.sl.au.repository.ChanceDeliveryRepository;
import com.sl.au.repository.ChanceManagerDeliveryRepository;
import com.sl.au.repository.ChanceRepository;
import com.sl.au.service.ChanceDeliveryService;
import com.sl.au.service.ChanceManagerDeliveryService;
import com.sl.au.service.QueryService;

@Service("ChanceManagerDeliveryService")
@Transactional
public class ChanceManagerDeliveryServiceImpl extends BaseServiceImpl implements ChanceManagerDeliveryService{

	@Autowired
	ChanceManagerDeliveryRepository chanceManagerDeliveryRepository;
	
	@Autowired
	QueryService queryService;
	
	@Override
	public ChanceManagerDeliveryRepository getRepository() {
		// TODO Auto-generated method stub
		return this.chanceManagerDeliveryRepository;
	}
	@Override
	public List<ChanceManagerDelivery> getModulePage(int start, int length, Map<String,String> condition, String orderName, String dir) {
		Page<ChanceManagerDelivery> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		pages = chanceManagerDeliveryRepository.findModuleUser(condition.get("chanceId"),condition.get("areaId"), pageable);
		return pages.getContent();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ChanceManagerDelivery> getPage(int start, int length,Map<String,Object> condition, String orderName, String dir) {
		String key = (String) condition.get("key");
		String isSucceed = (String)condition.get("isSucceed");
		String targetType = (String)condition.get("targetType");
		key = key.replaceAll("\\s+", "");
		StringBuilder hql = new StringBuilder("select c from ChanceManagerDelivery c where (c.user.id ='"+condition.get("userId")+"' "
				+ "and c.isReceived ='"+condition.get("isReceived")+"' "
				+ "and (c.saleChance.name like '%"+key+"%' "
				+ "or c.saleChance.source like '%"+key+"%' or c.saleChance.sourceUnit like '%"+key+"%' "
				+ "or c.saleChance.orderUnit like '%"+key+"%' or c.saleChance.orderPerson like '%"+key+"%'"
				+ "or c.saleChance.creator like '%"+key+"%'))");
		if(isSucceed != null && isSucceed.equals("已关闭")){
			hql.append("and c.saleChance.isSucceed is not NULL ");
		}else if(isSucceed != null && isSucceed.equals("处理中")){
			hql.append("and c.saleChance.isSucceed is NULL ");
		}
		if(targetType != null){
			hql.append("and c.targetType  = '"+ targetType+"'");
		}
		/*if(key.indexOf("处理中") != -1){
			hql.append("or c.saleChance.isSucceed is NULL ");
		}else{
			hql.append("or c.saleChance.isSucceed = '"+key+"'");	
		}*/
		hql.append(" order by "+orderName+" "+dir);
		Query query = queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return query.getResultList();
	}

	@Override
	public int getPageCount(Map<String, Object> condition) {
		String key = (String) condition.get("key");
		String isSucceed = (String)condition.get("isSucceed");
		String targetType = (String)condition.get("targetType");
		key = key.replaceAll("\\s+", "");
		StringBuilder hql = new StringBuilder("select c from ChanceManagerDelivery c where (c.user.id ='"+condition.get("userId")+"' "
				+ "and c.isReceived ='"+condition.get("isReceived")+"' "
				+ "and (c.saleChance.name like '%"+key+"%' "
				+ "or c.saleChance.source like '%"+key+"%' or c.saleChance.sourceUnit like '%"+key+"%' "
				+ "or c.saleChance.orderUnit like '%"+key+"%' or c.saleChance.orderPerson like '%"+key+"%'"
				+ "or c.saleChance.creator like '%"+key+"%')");
		if(isSucceed != null && isSucceed.equals("已关闭")){
			hql.append("and c.saleChance.isSucceed is not NULL ");
		}else{
			hql.append("and c.saleChance.isSucceed is NULL ");
		}
		if(targetType != null){
			hql.append("and c.targetType  = '"+ targetType+"'");
		}
		if(key.indexOf("处理中") != -1){
			hql.append(" or c.saleChance.isSucceed is NULL) ");
		}else{
			hql.append(" or c.saleChance.isSucceed = '"+key+"')");	
			}
		Query query = queryService.createQueryHql(hql.toString());
		return query.getResultList().size();
	}
}
